<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>菜单管理</title>
	<meta name="decorator" content="default"/>
	<%@include file="/WEB-INF/views/include/treeview.jsp" %>
	<style type="text/css">
		.nav-tabs li{cursor: pointer;}
		.ztree {overflow:auto;margin:0;_margin-top:10px;padding:10px 0 0 10px;}
	</style>
</head>

<body>
	<sys:message content="${message}"/>
	<div id="content" class="row-fluid">
		<div id="left" class="accordion-group">
			<div class="accordion-heading">
		    	<a class="accordion-toggle">菜单
		    		<!-- <input type="button" value="排序" onclick="sortTreeNodes()"> -->
		    		<i class="icon-refresh pull-right" onclick="refreshTree();"></i>
		    		
		    	</a>
		    </div>
			<div id="ztree" class="ztree"></div>
		</div>
		<div id="openClose" class="close">&nbsp;</div>
		<div id="right" >
			<ul class="nav nav-tabs">
				<li class="tabli active" link="iframeContent1"><a >菜单列表</a></li>
				<shiro:hasPermission name="sys:menu:edit">
					<li class="tabli " link="iframeContent2"><a >菜单详情</a></li>
				</shiro:hasPermission>
			</ul>
			<div id="iframeContentDiv">
				<iframe id="iframeContent1" name="iframe" url='' src="" width="100%" height="91%" frameborder="0"></iframe>
				<iframe id="iframeContent2" name="iframe" url='' src="" width="100%" height="91%" frameborder="0"></iframe>
			</div>
		</div>
	</div>
	
<script type="text/javascript">
	//URL  
	var listUrl = '${ctx}/sys/menu/listMenu?refer.type=${menu.refer.type}&refer.map=${menu.refer.map}&refer.dialog=${menu.refer.dialog}&sortType=${menu.sortType}&orderBy=${menu.orderBy}';
	var cardUrl = '${ctx}/sys/menu/formMenu';
	var treeUrl = '${ctx}/sys/menu/treeData';
	var treeId = 'ztree';
	var setting = {data:{simpleData:{enable:true,idKey:"id",pIdKey:"pId",rootPId:'0'}},
		callback:{onClick:function(event, treeId, treeNode){
				var id = treeNode.id == '0' ? '' :treeNode.id;
				var link = jQuery('#right').find('li.active').attr('link');
				//debugger;
				if(link=="iframeContent1"){
					var url = addUrlParam(listUrl,'parent.id',id);
					$('#iframeContent1').attr("src",url);
				}else if(link=="iframeContent2"){
					var btnSubmit = jQuery('#iframeContent2').contents().find('#btnSubmit')
					var url = addUrlParam(cardUrl,'id',id);
					if(btnSubmit.attr('disabled')!="disabled"){
						var flag = confirmx('要取消当前修改操作？',function(){
							freshTabs(jQuery('[link="iframeContent2"]'),url);
						});
						if(flag == true){
							return false;
						}
					}else{
						freshTabs(jQuery('[link="iframeContent2"]'),url);
					}
				}
			}
		}
	};
	
	var leftWidth = 180; // 左侧窗口大小
	var htmlObj = $("html"), mainObj = $("#main");
	var frameObj = $("#left, #openClose, #right, #right iframe");
	
	$(document).ready(function() {
		jQuery('#btnMenu').remove();
		refreshTree();
		jQuery("#iframeContent1").attr('url',listUrl);
		jQuery("#iframeContent2").attr('url',cardUrl);
		jQuery('.tabli').click(function(){
			changeTabs(this);
		});
		var	url = addUrlParam(listUrl,'parent.id','1');
		freshTabs(jQuery('[link="iframeContent1"]'),url);
		
	});
	
	
	function wSize(){
		var strs = getWindowSize().toString().split(",");
		htmlObj.css({"overflow-x":"hidden", "overflow-y":"hidden"});
		mainObj.css("width","auto");
		frameObj.height(strs[0] - 5);
		var leftWidth = ($("#left").width() < 0 ? 0 : $("#left").width());
		$("#right").width($("#content").width()- leftWidth - $("#openClose").width() -5);
		$(".ztree").width(leftWidth - 10).height(frameObj.height() - 46);
	}
	
	function refreshTree(){
		$.getJSON(treeUrl,function(data){
			var treeObj = $.fn.zTree.getZTreeObj(treeId);
			var status = getAllNodesStatus(treeObj);
			setDataStatus(data,status);
			var treeMode = $.fn.zTree.init($("#"+treeId), setting, data);
			//treeMode.expandAll(true);
			treeObj = $.fn.zTree.getZTreeObj(treeId);
			var nodes = treeObj.getNodes();
			if (nodes.length>0) {
				if(status!= null){
					//setOpen(treeObj,nodes[0],status.opens);
					setSelect(treeObj,nodes[0],status.selects);
				}else{
					treeObj.selectNode(nodes[0],true);
					var childrens = nodes[0].children;
					if(childrens!=null){
						//treeObj.expandNode(childrens[0], true, false, true);
						for(var i = 0 ; i < childrens.length ; i++){
							var childrenNode = childrens[i];
							treeObj.expandNode(childrenNode, true, false, true);
						}
						if(childrens[0].children!=null){
							treeObj.expandNode(childrens[0].children[0], true, false, true);
						}
					}
					
				}
			}
		});
	}
	//设置打开状态
	function setDataStatus(datas,status){
		if(datas==null || datas.length ==0 || status ==null ||status =='' || status == {}){
			return datas;
		}
		var treeObj = $.fn.zTree.getZTreeObj(treeId);
		for(var i = 0 ; i < datas.length ; i++){
			var data = datas[i];
			if(status.opens[data.id]!=null){
				data.open = true;
			}
		}
		return datas;
	}
	//动态打开节点
	function setOpen(treeObj,root,nodes){
		if(treeObj == null || root == null || nodes ==null){
			return ;
		}
		if(nodes[root.id]!=null){
			treeObj.expandNode(root,true,false,false);
		}
		var children = root.children;
		if(children == null){
			return ;
		}
		for(var i = 0 ; i < children.length ; i++){
			setOpen(treeObj,children[i],nodes);
		}
	}
	
	//根据刷新前的树节点选中当前节点
	function setSelect(treeObj,root,nodes){
		if(treeObj == null || root == null || nodes == null){
			return ;
		}
		if(nodes[root.id]!=null){
			treeObj.selectNode(root,true);
		}
		var children = root.children;
		if(children == null){
			return ;
		}
		for(var i = 0 ; i < children.length ; i++){
			setSelect(treeObj,children[i],nodes);
		}
	}
	
	//获取所有打开的和被选中的节点
	function getAllNodesStatus(treeObj){
		var status = null;
		if(treeObj == null){
			return status;
		}
		status = {};
		
		//open
		var opens = {};
		var nodes = treeObj.getNodes();
		for(var i = 0 ; i < nodes.length ; i++){
			var node = nodes[i];
			getOpenNodes(node,opens);
		}
		status.opens = opens;
		
		//selected
		var selects = {};
		var selectedNodes = treeObj.getSelectedNodes();
		for(var i = 0 ; i < selectedNodes.length ; i++){
			var selectedNode = selectedNodes[i];
			selects[selectedNode.id]= selectedNode;
		}
		status.selects = selects;
		
		return status;
	}
	
	function getOpenNodes(node,opens){
		if(opens == null){
			opens = {};
		}
		if(node == null){
			return opens;
		}
		if(node.open == true){
			opens[node.id] = node;
		}
		var children = node.children;
		if(children!=null){
			for(var i = 0 ; i < children.length ; i++){
				var child = children[i];
				if(child.isParent == true && child.children.length>0){
					getOpenNodes(child,opens);
				}
			}
		}
		return opens;
	}
	
	function getSelectNode(){
		var treeObj = $.fn.zTree.getZTreeObj(treeId);
		return treeObj.getSelectedNodes();
	}
		
	function freshTabs(obj,url){ //刷新页卡
		 jQuery('.tabli').removeClass('active');
		 jQuery(obj).addClass('active');
		 var iframeId = jQuery(obj).attr('link');
		 var iframeContents = jQuery('#iframeContentDiv').find('iframe');
		 iframeContents.css('display','none');
		 jQuery('#'+iframeId).css('display','');
		 
		 //debugger;
		 if(url!=null){
			 jQuery('#'+iframeId).attr('src',url);
		 }else{
			 jQuery('#'+iframeId).attr('src', jQuery('#'+iframeId).attr('url'));
		 }
	}
	
	function changeTabs(obj){ //切换页卡
		jQuery('.tabli').removeClass('active');
		 jQuery(obj).addClass('active');
		 var iframeId = jQuery(obj).attr('link');
		 var iframeContents = jQuery('#iframeContentDiv').find('iframe');
		 iframeContents.css('display','none');
		 jQuery('#'+iframeId).css('display','');
		 if(jQuery('#'+iframeId).attr('src') == null ||jQuery('#'+iframeId).attr('src') == ''){
			 jQuery('#'+iframeId).attr('src', jQuery('#'+iframeId).attr('url'));
		 }
	}
	
	function queryListPage(){
		 var iframeTabs = jQuery('[link="iframeContent1"]'); 
		 jQuery('.tabli').removeClass('active');
		 jQuery(iframeTabs).addClass('active');
		 var iframeId = jQuery(iframeTabs).attr('link');
		 var iframeContents = jQuery('#iframeContentDiv').find('iframe');
		 iframeContents.css('display','none');
		 jQuery('#'+iframeId).css('display','');
		 //jQuery('#'+iframeId).contents().find('#btnSubmit').click();
		 jQuery('#'+iframeId)[0].contentWindow.queryList();
	}
	
</script>
<c:if test='${menu.refer.type == 1}'>
<script type="text/javascript">
	function ok(data){ 
		parent.setReferValue(data);
	}
	function closeWindows(){ //关闭窗口
		var dialogId = '${menu.refer.dialog}'; 
		parent.closeWindows(dialogId);
	}
</script>
</c:if>

<script src="${ctxStatic}/common/wsize.min.js" type="text/javascript"></script>
</body>
</html>